package com.aqiang.day0528_shop.main.view.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.adapter.SecondAdapter
import com.aqiang.day0528_shop.main.adapter.StairAdapter
import com.aqiang.day0528_shop.main.contract.StairContract
import com.aqiang.day0528_shop.main.entity.BaseEntity
import com.aqiang.day0528_shop.main.entity.GoodsTypeEntity
import com.aqiang.day0528_shop.main.entity.NewEntity
import com.aqiang.day0528_shop.main.entity.T
import com.aqiang.day0528_shop.main.presenter.StairPresenter
import com.aqiang.day0528_shop.main.view.activity.GoodsActivity
import com.aqiang.lib_core.common.SLog
import com.aqiang.lib_core.mvp.BaseFragment
import kotlinx.android.synthetic.main.fragment_type.*
import kotlinx.coroutines.Deferred
import org.jetbrains.anko.startActivity

class TypeFragment : BaseFragment<StairContract.StairPresenter>(),StairContract.StairView {
    private var t:Int = 0
    private var second:Int = 16
    private var stairAdapter:StairAdapter? = null
    private var secondAdapter:SecondAdapter? = null
    private var dblclick:Boolean = false
    private var list:MutableList<T>? = null
    override fun bindLayout(): Int {
        return R.layout.fragment_type
    }

    override fun createPresenter() {
        mPresenter = StairPresenter(this)
    }

    override fun initView(savedInstanceState: Bundle?) {
        rv_frag_type_stair.layoutManager = LinearLayoutManager(context)
        rv_frag_type_second.layoutManager = LinearLayoutManager(context)
    }

    override fun initData() {
        doAsynTask {
            mPresenter!!.stair()
            mPresenter!!.second()
        }
    }

    override fun initEvent() {

    }

    override fun stairId(): Int {
        return 0;
    }

    override fun initStair(data: Deferred<BaseEntity<List<GoodsTypeEntity>>>) {
        doUiThread(data,{
            val data1 = it.data
            if(stairAdapter == null){
                stairAdapter = StairAdapter(R.layout.item_stair,data1)
                rv_frag_type_stair.adapter = stairAdapter
                stairAdapter?.setOnItemClickListener { adapter, view, position ->
                    second = data1.get(position).category_id
                    mPresenter!!.second()
                    secondAdapter!!.notifyDataSetChanged()
//                    if(dblclick){
//                        context?.startActivity<GoodsActivity>(
//                            "id" to second
//                        )
//                    }else{
//                        dblclick = true
//                    }
                }
                //dblclick = false
            }else{
                stairAdapter?.notifyDataSetChanged()
            }
        })
    }

    override fun secondId(): Int {
        return second
    }

    override fun initsecond(data: Deferred<BaseEntity<List<NewEntity>>>) {
        doUiThread(data,{
            val data1 = it.data
            for (item in data1){
                t = item.category_id
                doAsynTask {
                    mPresenter!!.T()
                }
                //mPresenter!!.T()
                if(list != null){
                    item.dada = list!!
                    //SLog.d("${list?.size}1111")
                }
            }
            if (secondAdapter == null){
                secondAdapter = SecondAdapter(R.layout.item_second_top,data1)
                rv_frag_type_second.adapter = secondAdapter
                secondAdapter?.setOnItemClickListener { adapter, view, position ->
                    context?.startActivity<GoodsActivity>(
                        "name" to data1.get(position).category_name,
                        "id" to data1.get(position).parent_id
                    )
                }
                ///secondAdapter?.data!!.addAll(data1)
            }else{
                secondAdapter?.data!!.clear()
                secondAdapter?.data!!.addAll(data1)
                secondAdapter?.notifyDataSetChanged()
            }
        })
    }

    override fun tId(): Int {
        return t
    }

    override fun initT(data: Deferred<BaseEntity<List<GoodsTypeEntity>>>) {
        list = mutableListOf();
        doUiThread(data,{
            val data1 = it.data
            if(data1 != null){
                for (item in data1){
                    val t1 = T(item.category_id,item.category_name,item.id,item.parent_id)
                    list!!.add(t1)
                    //SLog.d("${list.toString()}")
                }
            }
        })
    }
}